package com.quanyan.club.task.tracker;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.club.entity.db.TbChannelUser;
import com.quanyan.club.entity.db.TbChannelUserExample;
import com.quanyan.club.mapper.PullClubMapper;
import com.quanyan.club.mapper.TbChannelUserMapper;
import com.quanyan.common.utils.Limit;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by yue.yuan on 2017/2/23.
 */
@Component
public class PullClubSummaryJob implements JobRunner{

    private static Logger logger = Logger.getLogger(PullClubSummaryJob.class);

    @Autowired
    private TbChannelUserMapper tbChannelUserMapper;
    @Autowired
    private PullClubMapper pullClubMapper;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        //分页读取UID
        int pageSize = 20;
        TbChannelUserExample example = new TbChannelUserExample();
        int total = tbChannelUserMapper.countByExample(example);
        if(total == 0) {
            return new Result(Action.EXECUTE_SUCCESS,"没有新用户，PASS");
        }
        int successCount = 0;
        int all = 0;
        for (int i = 0; i < Math.ceil((double)total/pageSize); i++) {
            int pageNum = i + 1;
            Limit limit = Limit.buildLimit(pageNum, pageSize);
            example.setLimit(limit);
            List<Integer> uids = pullClubMapper.selectPullUid(limit.getStart(), limit.getSize());
            //根据UID查询俱乐部总数和活动总数以及俱乐部活动结算流水总和
            if(CollectionUtils.isEmpty(uids)) {
                continue;
            }
            for(Integer uid : uids) {
                if(null == uid || uid <= 0) {
                    continue;
                }
                updateChannelUserSummary(uid);
            }
        }

        return new Result(Action.EXECUTE_SUCCESS);
    }

    private void updateChannelUserSummary(int uid) {
        List<Integer> clubIds = pullClubMapper.selectPullClubsByUid(uid);
        if(CollectionUtils.isEmpty(clubIds)) {
            return;
        }
        int totalClubs = clubIds.size();
        int totalActivity = pullClubMapper.selectPullActivitiesByUid(uid);
        BigDecimal totalAmount = BigDecimal.ZERO;
        if(totalActivity > 0) {
            totalAmount = pullClubMapper.selectPullAmountByClubs(clubIds);
        }
        TbChannelUserExample example = new TbChannelUserExample();
        example.createCriteria().andUidEqualTo(uid);
        TbChannelUser update = new TbChannelUser();
        update.setTotalActivityCount(totalActivity);
        update.setTotalClubCount(totalClubs);
        update.setTotalAmount(totalAmount);
        int updateCount = tbChannelUserMapper.updateByExampleSelective(update, example);
        logger.warn("更新一条拉新用户统计记录完成，成功数：" + updateCount);
    }
}
